import { SHOW_GLOBAL_ALERT, HIDE_GLOBAL_ALERT,SUCCESS_ALERT,ERROR_ALERT } from './actionType'
const defaultValue = {
  isAlerting: false,
  alertingMsg: 'asdfadsf',
  alertType: 'success'
}

const reducer = (state = defaultValue, action) => {
  const newState = JSON.parse(JSON.stringify(state))
  switch (action.type) {
    case SHOW_GLOBAL_ALERT:
      newState.isAlerting = true
      newState.alertingMsg = action.value.msg
      newState.alertType = action.value.type
      break;
    case HIDE_GLOBAL_ALERT:
      newState.isAlerting = false
      break;
    case SUCCESS_ALERT:
      newState.isAlerting = true
      newState.alertType = "success"
      newState.alertingMsg = action.value
      setTimeout(() => {
        newState.isAlerting = false
      }, 1500);
      break

    default:
      break;
  }
  return newState
}

export default reducer